package com.iocollege.tinyframe.webapi.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.primitives.Longs;
import com.iocollege.tinyframe.common.MyBaseController;
import com.iocollege.tinyframe.common.annotation.Log;
import com.iocollege.tinyframe.common.annotation.RepeatSubmit;
import com.iocollege.tinyframe.common.core.AjaxResult;
import com.iocollege.tinyframe.common.core.constant.BusinessType;
import com.iocollege.tinyframe.common.core.constant.UserConstants;
import com.iocollege.tinyframe.common.core.domain.QueryPagePara;
import com.iocollege.tinyframe.common.utils.QueryParaHelper;
import com.iocollege.tinyframe.common.utils.SecurityUtils;
import com.iocollege.tinyframe.entity.system.SysConfig;
import com.iocollege.tinyframe.service.system.SysConfigService;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/system/config")
public class SysConfigApiController extends MyBaseController {

    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 获取参数配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @RequestMapping("/list")
    public AjaxResult list(String where,Integer pageNum,Integer pageSize,String sort,String desc){

        QueryPagePara queryPagePara = QueryParaHelper.GenQuery(where, pageSize, pageNum, sort, desc);
        IPage<SysConfig> sysConfigIPage=sysConfigService.getByPage(queryPagePara);
        return AjaxResult.success(sysConfigIPage);
    }


    /**
     * 根据参数编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable Long configId)
    {
        return AjaxResult.success(sysConfigService.getById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public AjaxResult getConfigKey(@PathVariable String configKey)
    {
        return AjaxResult.success(sysConfigService.findByAttrName("config_key",configKey));
    }

    /**
     * 新增参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@Validated @RequestBody SysConfig config)
    {
        if (UserConstants.NOT_UNIQUE.equals(sysConfigService.checkConfigKeyUnique(config)))
        {
            return AjaxResult.error("新增参数'" +  config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setCreateBy(SecurityUtils.getUsername());
        return AjaxResult.success(sysConfigService.save(config));
    }

    /**
     * 修改参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysConfig config)
    {
        if (UserConstants.NOT_UNIQUE.equals(sysConfigService.checkConfigKeyUnique(config)))
        {
            return AjaxResult.error("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setUpdateBy(SecurityUtils.getUsername());
        return AjaxResult.success(sysConfigService.updateById(config));
    }

    /**
     * 删除参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds)
    {
        List<Long> longList = Arrays.asList(configIds);

        return AjaxResult.success(sysConfigService.removeByIds(longList));
    }

    /**
     * 清空缓存
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clearCache")
    public AjaxResult clearCache()
    {
        sysConfigService.clearCache();
        return AjaxResult.success();
    }



}
